#ifndef _NAND_H
#define _NAND_H

/* NAND Flash registers 2440*/
#define NFCONF    (*(volatile unsigned int *)0x4e000000)
#define NFCONT    (*(volatile unsigned int *)0x4e000004)

#define NFCMD    (*(volatile unsigned char *)0x4e000008)
#define NFADDR    (*(volatile unsigned char *)0x4e00000c)

#define NFDATA    (*(volatile unsigned char *)0x4e000010)
#define NFSTAT    (*(volatile unsigned char *)0x4e000020)
#define NFMECC0	(*(volatile unsigned *)0x4E00002C)

#define NAND_CHIP_ENABLE (NFCONT &= ~(1<<1))
#define NAND_CHIP_DISABLE (NFCONT |= (1<<1))

#define NAND_CLEAR_RB (NFSTAT |= (1<<2))
#define NAND_DETECT_RB { while(! (NFSTAT&(1<<0)) );}

#define NAND_SECTOR_SIZE 512
#define NAND_BLOCK_MASK  (NAND_SECTOR_SIZE - 1)

#define BUSY 1

void init_nand(void);
void reset_nand(void);
void copy_myself(void);

unsigned int WaitNFBusy(void);
unsigned int EraseBlock(unsigned int addr);

void ReadPage(unsigned int addr, unsigned char *buf);
int WritePage(int addr_flash, unsigned char *addr_sdram);
int nand_read_ll(unsigned char *buf, unsigned long start_addr, int size);

#endif
